﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class SalesItemTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("sku", 1)]
        public void Can_CRUD(string sku, decimal price)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;
            var catalogItem = Processor.Execute(new CreateCatalogItem
            {
                SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID
            }).Result;

            #endregion

            var item = Processor.Execute(new CreateSalesItem
            {
                SellerID = seller.EntityID,
                CatalogItemID = catalogItem.EntityID,
                SKU = sku,
                Price = price
            }).Result;

            Assert.NotNull(item);
            item.SKU.AreEqualIgnoringCase(sku);
            item.Price.AreEqual(price);
            item.AreEqual(Processor.Execute(new GetSalesItem
            {
                EntityID = item.EntityID
            }).Result);
            item.AreEqual(Processor.Execute(new GetSalesItemBySKU
            {
                SKU = sku
            }).Result);

            sku = sku.Reverse();
            price = price * 2;
            var upd_item = Processor.Execute(new UpdateSalesItem
            {
                EntityID = item.EntityID,
                SellerID = seller.EntityID,
                CatalogItemID = item.CatalogItem.EntityID,
                SKU = sku,
                Price = price,
                Flags = SalesItemFlags.Active
            }).Result;

            Assert.NotNull(upd_item);
            upd_item.AreEqual(item);
            upd_item.SKU.AreEqualIgnoringCase(sku);
            upd_item.Price.AreEqual(price);
            upd_item.Flags.AreEqual(SalesItemFlags.Active);

            Processor.Execute(new DeleteSalesItem
            {
                EntityID = item.EntityID
            });
        }

        [Test]
        public void Cannot_CreateUpdate_If_SKU_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateSalesItem
            {
                SKU = String.Empty,
                Price = 1
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateSalesItem
            {
                SKU = String.Empty,
                Price = 1
            }));
        }

        [Test]
        public void Cannot_CreateUpdate_If_Price_Lt_Zero()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateSalesItem
            {
                SKU = "xy",
                Price = -1
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateSalesItem
            {
                SKU = "xy",
                Price = -1
            }));
        }

        [Test]
        public void Can_Manage_Option_Relations()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;

            #endregion

            var option = Processor.Execute(new CreateSalesOption
            {
                CodeName = "xy",
                CodeValue = "xy",
                Flags = SalesOptionFlags.Item
            }).Result;
            var item = Processor.Execute(new CreateSalesItem
            {
                SellerID = seller.EntityID,
                CatalogItemID = Processor.Execute(new CreateCatalogItem
                {
                    SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID
                }).Result.EntityID,
                SKU = "xy",
                Price = Decimal.Zero
            }).Result;

            Processor.Execute(new MapSalesOptionToSalesItem
            {
                ChildID = option.EntityID,
                EntityID = item.EntityID
            }).Result.Options
                .DoesContain(option);

            Processor.Execute(new UnmapSalesOptionFromSalesItem
            {
                ChildID = option.EntityID,
                EntityID = item.EntityID
            }).Result.Options
                .DoesNotContain(option);
        }

        [Test]
        public void Can_Manage_Rule_Relations()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;

            #endregion

            var rule = Processor.Execute(new CreateFixedTaxRule
            {
                Amount = 10,
                Name = "xy",
                ValidFrom = DateTime.UtcNow,
                ValidTo = DateTime.UtcNow.AddHours(1),
                CommonFlags = SalesRuleFlags.Active | SalesRuleFlags.ItemScope
            }).Result;
            var item = Processor.Execute(new CreateSalesItem
            {
                SellerID = seller.EntityID,
                CatalogItemID = Processor.Execute(new CreateCatalogItem
                {
                    SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID
                }).Result.EntityID,
                SKU = "xy",
                Price = Decimal.Zero
            }).Result;

            Processor.Execute(new MapSalesRuleToSalesItem
            {
                ChildID = rule.EntityID,
                EntityID = item.EntityID
            }).Result.Rules
                .DoesContain(rule);

            Processor.Execute(new UnmapSalesRuleFromSalesItem
            {
                ChildID = rule.EntityID,
                EntityID = item.EntityID
            }).Result.Rules
                .DoesNotContain(rule);
        }
    }
}
